package com.situ.web.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.situ.web.common.ServerResponse;
import com.situ.web.entity.Banji;
import com.situ.web.entity.PageBean;
import com.situ.web.entity.Student;
import com.situ.web.entity.User;
import com.situ.web.service.IStudentService;
import com.situ.web.service.impl.StudentServiceImpl;
import com.situ.web.util.JSONUtil;

@WebServlet("/student")
public class StudentServlet extends HttpServlet {

	private IStudentService studentService = new StudentServiceImpl();
	
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 解决乱码问题
		resp.setContentType("text/html;charset=utf-8");
//		req.setCharacterEncoding("utf-8");
		// 登录验证
		HttpSession session = req.getSession();
		User user = (User) session.getAttribute("user");
		if (null == user) {
			// 用户没有登录，重定向登录界面
			resp.sendRedirect(req.getContextPath()+"/login.jsp");
			return;
		}
		
		String method = req.getParameter("method");
		if (method == null || "".equals(method)) {
			method = "pageList";
		}
		switch (method) {
		case "selectAll":
			selectAll(req, resp);
			break;
		case "pageList":
			pageList(req, resp);
			break;
		case "insert":
			insert(req, resp);
			break;
		case "deleteById":
			deleteById(req, resp);
			break;
		case "selectById":
			selectById(req, resp);
			break;
		case "selectById2":
			selectById2(req, resp);
			break;
		case "update":
			update(req, resp);
			break;
		case "search":
			search(req, resp);
			break;
		case "checkName":
			checkName(req, resp);
			break;
		case "getBanjiId":
			getBanjiId(req, resp);
			break;
		case "selectStudentBanjiInfo":
			selectStudentBanjiInfo(req, resp);
			break;
		default:
			break;
		}
	}

	private void selectStudentBanjiInfo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		List<Map<String, Object>> list = studentService.selectStudentBanjiInfo();
		req.setAttribute("list", list);
		req.getRequestDispatcher("student_banji_list.jsp").forward(req, resp);
	}

	private void getBanjiId(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//		ArrayList<Banji> list = new ArrayList<Banji>();
//		Banji banji1 = new Banji(1,"Java190701");
//		Banji banji2 = new Banji(2,"Java190702");
//		list.add(banji1);
//		list.add(banji2);
		ArrayList<Banji> list = studentService.selectBanjiAll();
		
		
		req.setAttribute("list", list);
		req.getRequestDispatcher("student_insert.jsp").forward(req, resp);
	}

	private void checkName(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String name = req.getParameter("nameInput");
		boolean isExist = studentService.checkName(name);
		resp.getWriter().write("{\"isExist\":"+isExist+"}");
	}

	private void pageList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// http://localhost:8080/JavaWeb1907/student?method=pageList&pageNo=2&pageSize=3
				String pageNo = req.getParameter("pageNo");
				String pageSize = req.getParameter("pageSize");
				
				if (null == pageNo || "".equals(pageNo)) {
					pageNo = "1";
				}
				if (null == pageSize || "".equals(pageSize)) {
					pageSize = "3";
				}
				
				PageBean pageBean = studentService.pageList(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
				
				req.setAttribute("pageBean", pageBean);
				req.getRequestDispatcher("student_page_list.jsp").forward(req, resp);
	}

	private void selectById2(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		String id = req.getParameter("id");
		Student student = studentService.selectById2(Integer.parseInt(id));
		// 放到一块内存区域里面
		req.setAttribute("student", student);
		// 请求转发到student_update.jsp页面进行展示
		req.getRequestDispatcher("/student_update.jsp").forward(req, resp);
	}

	private void search(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String searchName = req.getParameter("searchName");
		ArrayList<Student> list = (ArrayList<Student>) studentService.search(searchName);
		// 放到一块内存区域里面
		req.setAttribute("list", list);
		// 请求转发到student_list.jsp页面进行展示
		req.getRequestDispatcher("/student_list.jsp").forward(req, resp);
	}

	private void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		ArrayList<Student> list = (ArrayList<Student>) studentService.selectAll();

		// 放到一块内存区域里面
		req.setAttribute("list", list);
		// 请求转发到student_list.jsp页面进行展示
		req.getRequestDispatcher("/student_list.jsp").forward(req, resp);

	}

	private void insert(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String name = req.getParameter("name");
		String age = req.getParameter("age");
		String gender = req.getParameter("gender");
		String banjiId = req.getParameter("banjiId");
		
		Student student = new Student( name,Integer.parseInt(age),gender,Integer.parseInt(banjiId));
		boolean isSuccess = studentService.insert(student);
		ServerResponse serverResponse = null;
		if(isSuccess) {
			serverResponse = ServerResponse.createSuccess("添加成功");
		}else {
			serverResponse = ServerResponse.createError("添加失败");
		}
		JSONUtil.obj2Json(resp, serverResponse);
		// 删除完了之后重定向到列表界面再展示所有数据
		//resp.sendRedirect(req.getContextPath() + "/student?method=selectAll");
	}

		// 重定向到/JavaWeb1907/student
		//resp.sendRedirect("/JavaWeb1907/student");
	

	private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		boolean isSuccess = studentService.deleteById(Integer.parseInt(id));
		
		ServerResponse serverResponse = null;
		if(isSuccess) {
			serverResponse = ServerResponse.createSuccess("删除成功");
		}else {
			serverResponse = ServerResponse.createError("删除失败");
		}
		JSONUtil.obj2Json(resp, serverResponse);
		// 删除完了之后重定向到列表界面再展示所有数据
		//resp.sendRedirect(req.getContextPath() + "/student?method=selectAll");
	}

	private void selectById(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {

		String iD = req.getParameter("id");
		ArrayList<Student> list = (ArrayList<Student>) studentService.selectById(Integer.parseInt(iD));
		// 放到一块内存区域里面
		req.setAttribute("list", list);
		// 请求转发到student_list.jsp页面进行展示
		req.getRequestDispatcher("/student_list.jsp").forward(req, resp);
	}

	private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		String name = req.getParameter("name");
		String age = req.getParameter("age");
		String gender = req.getParameter("gender");
		String banjiId = req.getParameter("banjiId");
		Student student = new Student(Integer.parseInt(id), name, Integer.parseInt(age), gender,Integer.parseInt(banjiId));
		boolean isSuccess = studentService.update(student);
		
		ServerResponse serverResponse = null;
		if(isSuccess) {
			serverResponse = ServerResponse.createSuccess("更改成功");
		}else {
			serverResponse = ServerResponse.createError("更改失败");
		}
		JSONUtil.obj2Json(resp, serverResponse);
		// 删除完了之后重定向到列表界面再展示所有数据
		//resp.sendRedirect(req.getContextPath() + "/student?method=selectAll");
	}
		//resp.sendRedirect("/JavaWeb1907/student?method=selectAll");
	
}
